package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseConText;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart>add(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() !=null,"setmeal_id",shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseConText.getCurrentId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart != null){
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }
        else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseConText.getCurrentId());
            cart=shoppingCart;
            shoppingCartService.save(cart);

        }
        return R.success(cart);
    }
    @GetMapping("/list")
    public R<List<ShoppingCart>>list(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseConText.getCurrentId());
        queryWrapper.orderByAsc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseConText.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车");
    }
    @PostMapping("/sub")
    public R<ShoppingCart>sub(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() !=null,"setmeal_id",shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseConText.getCurrentId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart.getNumber() == 1){
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.remove(queryWrapper);
        }
        else {
            cart.setNumber(cart.getNumber()-1);
           shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

}